package com.gage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gage.commons.result.PageInfo;
import com.gage.commons.result.dictsTree;
import com.gage.mapper.SysBaseDictMapper;
import com.gage.model.SysBaseDict;
import com.gage.service.ISysDictService;

@Transactional(rollbackFor = Exception.class)
@Service
public class SysDictServiceImpl extends ServiceImpl<SysBaseDictMapper, SysBaseDict> implements ISysDictService
{
	@Autowired
	private SysBaseDictMapper dictMapper;
	private List<String> temp = new ArrayList<String>();
	@Override
	public String selectDictsTree(String pid) {
		List<dictsTree> lt = new ArrayList<dictsTree>();
		List<SysBaseDict> list = new ArrayList<SysBaseDict>();
		list = dictMapper.findByPid(pid);
		for (SysBaseDict o : list)
		{
			dictsTree t = new dictsTree();
			t.setId(o.getDictcode());
			t.setText(o.getDictname());
			int count = dictMapper.getCountChild(o.getDictcode());
			if (count > 0)
			{
				t.setState("closed");
			} else
			{
				t.setState("open");
			}
			lt.add(t);
		}

		JSONArray array = JSONArray.fromObject(lt);
		return array.toString();
	}

	@Override
	public List<SysBaseDict> selectDictsTreeGrid() {
		 EntityWrapper<SysBaseDict> wrapper = new EntityWrapper<SysBaseDict>();
	        wrapper.orderBy("seq");
	        return dictMapper.selectList(wrapper);
	}
	@Override
	public boolean insert(SysBaseDict entity) {
		if (dictMapper.insert(entity)>0) {
			return true;
		}
		return false;
	}
	@Override
	public List<SysBaseDict> findDictByPid(String pid) {
		return dictMapper.findByPid(pid);
	}
	@Override
	public boolean insertAllColumn(SysBaseDict entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean insertBatch(List<SysBaseDict> entityList) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean insertBatch(List<SysBaseDict> entityList, int batchSize) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean insertOrUpdateBatch(List<SysBaseDict> entityList) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean insertOrUpdateBatch(List<SysBaseDict> entityList,
			int batchSize) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateById(SysBaseDict entity) {
		if (dictMapper.updateById(entity)>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateAllColumnById(SysBaseDict entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(SysBaseDict entity, Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateBatchById(List<SysBaseDict> entityList) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateBatchById(List<SysBaseDict> entityList, int batchSize) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean insertOrUpdate(SysBaseDict entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SysBaseDict selectOne(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> selectMap(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object selectObj(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int selectCount(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<SysBaseDict> selectList(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<SysBaseDict> selectPage(Page<SysBaseDict> page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> selectMaps(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Object> selectObjs(Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<Map<String, Object>> selectMapsPage(Page page,
			Wrapper<SysBaseDict> wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<SysBaseDict> selectPage(Page<SysBaseDict> page,
			Wrapper<SysBaseDict> wrapper) {
		 SqlHelper.fillWrapper(page, wrapper);
	        page.setRecords(baseMapper.selectPage(page, wrapper));
	        return page;
	}

	@Override
	public PageInfo selectDataGrid(PageInfo pageInfo) {
		 Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
	        page.setOrderByField(pageInfo.getSort());
	        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
	        List<Map<String, Object>> list = dictMapper.selectDictsPage(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal(page.getTotal());
	        return pageInfo;
	}

	@Override
	public SysBaseDict getDictByDictcode(String dictcode) {
		return dictMapper.getDictByDictcode(dictcode);
	}

	@Override
	public SysBaseDict getDictById(String id) {
		return dictMapper.selectById(id);
	}

	@Override
	public int getCountChild(String id) {
		int count=0;
		count=dictMapper.getCountChild(id);
		return count;
	}

	@Override
	public boolean removeDict(List<String> ids) {
		try
		{
			if (this.dictMapper.deleteByIds(ids) > 0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e)
		{
			System.err.println(e.getMessage());
			return false;
		}
	}

	@Override
	public int updateMoveDict(String dictId, String parentid) {
		try{
			//Integer cid=Integer.parseInt(dictId);
			//Integer pid=Integer.parseInt(parentid);
			int i=0,olevel=0,plevel=0;
			List<String> cids=this.getAllChildOrgaIdList(dictId);
			if (!"0".equals(parentid)&&cids!=null&&cids.size()>0){
				for(String id:cids){
					if (parentid.equals(id.toString())){
						return 0;
					}
				}
			}
			plevel=this.getLevel(parentid);
			SysBaseDict o=dictMapper.getDictByDictcode(dictId);
			olevel=o.getDictlevel();
			i=plevel-olevel;
			o.setParentid(parentid);
			o.setDictlevel(plevel);

			dictMapper.updateById(o);
			if (cids!=null&&cids.size()>0){
				dictMapper.updateDictLevel(cids,i);
			}
			return 1;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return 0;
		}
	}
	public List<String> getAllChildOrgaIdList(String parentid) {
		temp = new ArrayList<String>();
		initChildDictId(parentid.toString());
		return temp;
	}
	private void initChildDictId(String parentid){
		List<String> list = new ArrayList<String>();
		list = dictMapper.getChildDictcodeList(parentid);
		for(String element : list){
			temp.add(element);
			if (dictMapper.getCountChild(element.toString())>0){
				initChildDictId(element.toString());
			}
		}
	}
/**
 * 获取数据字典的树节点的等级
 * @param dictcode
 * @return
 */
	public int getLevel(String dictcode) {
		try{
			Integer i= this.dictMapper.getLevel(dictcode);
			if (i==null){
				return 1;
			}else{
				return i+1;
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
			return 1;
		}
	}
}
